﻿/*
 * Copyright 2008 Pimm Hogeling, Karim Sempel and Demian Sempel
 * 
 * This file is part of 8bitplays.
 * 
 * 8bitplays is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or (at your
 * option) any later version.
 * 
 * 8bitplays is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with 8bitplays. If not, see <http://www.gnu.org/licenses/>.
 */
package nl.sandmen.team.main {
	import flash.desktop.NativeApplication;
	import flash.desktop.SystemTrayIcon;
	import flash.display.NativeWindow;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	/**
	 * @author Team Sandmen
	 */
	public class TitleController {
		private static var _title:String;
		private static var _window:NativeWindow;
		private static var currentTitlePos:uint;
		private static var supportsSystemTrayIcon:Boolean;
		private static var titleScroller:Timer;
		public static function initialize(window:NativeWindow):void {
			_window = window;
			supportsSystemTrayIcon = NativeApplication.supportsSystemTrayIcon;
			titleScroller = new Timer(300);
			titleScroller.addEventListener(TimerEvent.TIMER, scroll);
		}
		private static function scroll(e:TimerEvent):void {
			var display:String = _title.substr(currentTitlePos, 16);
			if (display.length < 16) {
				display += _title.substr(0, 16 - display.length);
			}
			_window.title = display;
			if (++currentTitlePos === _title.length) {
				currentTitlePos = 0;
			}
		}
		public static function setTitle(title:String):void {
			_title = title + " - ";
			if (supportsSystemTrayIcon) {
				(NativeApplication.nativeApplication.icon as SystemTrayIcon).tooltip = title;
			}
			currentTitlePos = 0;
			titleScroller.start();
		}
		public static function stop():void {
			titleScroller.removeEventListener(TimerEvent.TIMER, scroll);
		}
	}
}